package com.schedule.service.impl;

import com.github.pagehelper.PageHelper;
import com.schedule.Util.PagedGridResult;
import com.schedule.mapper.MessageMapper;
import com.schedule.pojo.Admin;
import com.schedule.pojo.Message;
import com.schedule.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private AdminServiceImpl adminService;

    /**
     * 分页查询所有裁判
     * page: 第几页
     * pageSize: 每页显示条数
     */
    @Override
    public PagedGridResult findAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Message> judges = messageMapper.findAll();
        PagedGridResult gridResult = PagedGridResult.setterPagedGrid(judges, page);
        return gridResult;
    }

    /**
     * 设置信息
     *
     * @param Content
     * @param date
     */
    @Override
    public boolean insertMessage(String Title, String Content, Date date) {
        Admin currentAdmin = adminService.getCurrentAdmin();
        if (currentAdmin == null){
            return false;
        }
        Integer id = currentAdmin.getId();
        String username = currentAdmin.getUsername();
        if (messageMapper.insertMessage(Title, username, Content, date, id)!=0){
            return true;
        }
        return false;
    }


    /**
     * 删除信息
     */
    @Override
    public boolean delMessage(Integer Mid) {
        if (messageMapper.delMessage(Mid)!=0){
            return true;
        }
        return false;
    }

    /**
     * 更新信息
     */
    @Override
    public boolean updateMessage(String Title, String Content, Date date, Integer Mid) {
        Admin currentAdmin = adminService.getCurrentAdmin();
        if (currentAdmin == null){
            return false;
        }
        Integer id = currentAdmin.getId();
        String username = currentAdmin.getUsername();
        if (messageMapper.updateMessage(Title, username, Content, date, id, Mid)!=0){
            return true;
        }
        return false;
    }

}
